#!/usr/bin/python
# grammar binarizer
#

_p="V"
_c=0

def isconsecutive(a,b):
	return a[1]==b[0]-1 or b[1]==a[0]-1

def combine(a,b):
	print a[0],a[1],b[0],b[1]
	if a[0]<b[0]:
		return (a[0],b[1])
	else:
		return (b[0],a[1])

def binarize(a):
	stack = []
	stack.append(a[0])
	for i in xrange(1,len(a)):
		stack.append(a[i])
		while len(stack) > 1 and isconsecutive(stack[-2],stack[-1]):
			x = combine(stack[-1],stack[-2])
			stack.pop()
			stack.pop()
			stack.append(x)
	return stack[-1][0] == 1

a = [1,5,3,4,2]
b=[]
for i in a:
	b.append((i,i))
print binarize(b)

